package org.jradweb.core.utils;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.ParseException;
import java.util.Date;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.jradweb.core.orm.Model;

public class BeanUtils {
	private static Logger log=Logger.getLogger(BeanUtils.class);
	
	private String MESSAGE_KEY_NOT_INTEGER="validator.type.notInteger";
	private String MESSAGE_KEY_NOT_NUMBER="validator.type.notInteger";
	private String MESSAGE_KEY_NOT_DATE="validator.type.notInteger";
	
	public static void setProperties(Model model, Map<String, String> params) {
		for (String key : params.keySet()) {
			Method m = getMethod(key, model.getClass());
			if (m != null && !m.getName().equals("setId")
					&& !m.getName().equals("setUpdatedAt")
					&& !m.getName().equals("setCreatedAt")) {

				Class[] pts = m.getParameterTypes();
				if (pts.length == 1) {
					try {
						if (pts[0].equals(String.class)) {
							Object value=params.get(key);
							String strValue=(String)params.get(key);
							m.invoke(model, strValue);
						} else if (pts[0].equals(Integer.class)) {
							m.invoke(model, Integer.parseInt(params.get(key)));
						} else if (pts[0].equals(Date.class)) {
							Date date = DateFormat.parseDate(params.get(key));
							m.invoke(model, date);
						}
					} catch (IllegalArgumentException e) {
						e.printStackTrace();
					} catch (IllegalAccessException e) {
						e.printStackTrace();
					} catch (InvocationTargetException e) {
						e.printStackTrace();
					} catch (ParseException e) {
						e.printStackTrace();
					}
				}
			}
		}
		// need convert date time value if the server time is not GMT
		model.setUpdatedAt(new Date());
	}
	public static void setProperties(Model model, HttpServletRequest request) {
		
		if(model.getId()==0){
			try {
				String strId=request.getParameter("id");
				if(strId!=null){
					int id=Integer.parseInt(strId);
					model.setId(id);
				}
			} catch (NumberFormatException e) {
				log.warn("The value of parameter id \""+request.getParameter("id")+"\" is not a number.", e);
			}
			
		}
		
		for (String key : (Set<String>)request.getParameterMap().keySet()) {
			Method m = getMethod(key, model.getClass());
			if (m != null && !m.getName().equals("setId")
					&& !m.getName().equals("setUpdatedAt")
					&& !m.getName().equals("setCreatedAt")&&m.getName().startsWith("set")) {

				Class[] pts = m.getParameterTypes();
				if (pts.length == 1) {
					try {
						if (pts[0].equals(String.class)) {
							 
							String strValue=(String)request.getParameter(key);
							m.invoke(model, strValue);
						} else if (pts[0].equals(Integer.class)) {
							m.invoke(model, Integer.parseInt(request.getParameter(key)));
						} else if (pts[0].equals(Date.class)) {
							Date date = DateFormat.parseDate(request.getParameter(key));
							m.invoke(model, date);
						}

					} catch (IllegalArgumentException e) {
						e.printStackTrace();
					} catch (IllegalAccessException e) {
						e.printStackTrace();
					} catch (InvocationTargetException e) {
						e.printStackTrace();
					} catch (ParseException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		}
		// need convert date time value if the server time is not GMT
		model.setUpdatedAt(new Date());
	}

	private static Method getMethod(String propertyName, Class modelClass) {
		for (Method m : modelClass.getMethods()) {

			if (m.getName().equals(
					"set" + StringUtils.upperCaseFirst(propertyName))) {
				return m;
			}
		}
		return null;
	}

}
